package com.coolwar.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * @author Coolwar
 */
@Aspect
@Component
@Order(2)
public class AAspect {

    /**
     * 此处的切点是注解的方式，也可以用包名的方式达到相同的效果
     * '@Pointcut("execution(* com.wwj.springboot.service.impl.*.*(..))")'
     */
    @Pointcut("@annotation(com.coolwar.aspect.Ann)")
    public void pointcut() {
    }

    /**
     * 相当于MethodInterceptor
     */
    @Around("pointcut()")
    public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
        long time = System.currentTimeMillis();
        System.out.println("A执行时间：" + time);
        Object res = joinPoint.proceed();
        time = System.currentTimeMillis() - time;
        System.out.println("A执行耗时：" + time);
        return res;
    }

    @Before("pointcut()")
    public void doBeforeAdvice(JoinPoint joinPoint) {
        System.out.println("A进入方法前执行.....");

    }

    /**
     * 处理完请求，返回内容
     */
    @AfterReturning(returning = "ret", pointcut = "pointcut()")
    public void doAfterReturning(Object ret) {
        System.out.println("A方法的返回值 : " + ret);
    }

    /**
     * 后置异常通知
     */
    @AfterThrowing("pointcut()")
    public void throwss(JoinPoint jp) {
        System.out.println("A方法异常时执行.....");
    }


    /**
     * 后置最终通知不管是抛出异常或者正常退出都会执行
     */
    @After("pointcut()")
    public void after(JoinPoint jp) {
        System.out.println("A出方法后执行.....");
    }

}
